package com.pyue.eam.regi.module.region.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.pyue.eam.admin.api.entity.eam.EamRegion;
import com.pyue.eam.admin.api.entity.regi.CqTaskLibEntity;
import com.pyue.eam.common.core.util.SpringContextHolder;
import com.pyue.eam.regi.module.region.mapper.EamRegionMapper;

import java.math.BigDecimal;

public class TaskPriceUtil {

    // 计件
    public static String priceRange(CqTaskLibEntity cqTaskLib) {
        if (StrUtil.isNotBlank(cqTaskLib.getAddress()) && StrUtil.isNotBlank(cqTaskLib.getAddress())) {
            EamRegion region = getEamRegion(cqTaskLib.getAddress());

            String tactics = cqTaskLib.getTactics();
            if ("1".equals(tactics)) {
                // 计时 todo 参数配置
                Double minHourPrice = privatePriceByHour(cqTaskLib.getHourPrice(), cqTaskLib.getPerNum(), region.getHourMin());
                Double maxHourPrice = privatePriceByHour(cqTaskLib.getHourPrice(), cqTaskLib.getPerNum(), region.getHourMax());
                return StrUtil.concat(Boolean.TRUE, minHourPrice.toString(), "～", maxHourPrice.toString());
            } else if ("2".equals(tactics)) {
                // 计件
                return privatePriceByPer(cqTaskLib.getHourPrice(), cqTaskLib.getPlanPartNum()).toString();
            } else if ("3".equals(tactics)) {
                // 记天
                Double minDayPrice = privatePriceByDay(cqTaskLib.getPerNum(), cqTaskLib.getHourPrice(), region.getDayMin());
                Double maxDayPrice = privatePriceByDay(cqTaskLib.getPerNum(), cqTaskLib.getHourPrice(), region.getDayMax());
                return StrUtil.concat(Boolean.TRUE, minDayPrice.toString(), "～", maxDayPrice.toString());
            }
            return "0";
        }
        return null;
    }

    public static EamRegion getEamRegion(String address) {
        String city = Convert.toStr(StrUtil.split(address, "/").get(1), "");
        return SpringContextHolder.getBean(EamRegionMapper.class).getPriceByCityName(city);
    }

    private static Double privatePriceByDay(Integer perNum, Double hourPrice, BigDecimal dayMin) {
        return hourPrice * Convert.toInt(perNum, 0) * dayMin.doubleValue();
    }

    private static Double privatePriceByHour(Double hourPrice, Integer perNum, BigDecimal price) {
        // 计时 todo 参数配置 不足四小时按四小时算
        hourPrice = hourPrice >= 4 ? hourPrice : 4;
        return hourPrice * perNum * price.doubleValue();
    }

    private static Double privatePriceByPer(Double hourPrice, Integer planPartNum) {
        return hourPrice * Convert.toInt(planPartNum, 0);
    }

}
